﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Data.Services.Client;
using Microsoft.Phone.Controls;
using RestSharp;
using Wp7Anonymous.SampleData;

namespace Wp7Anonymous
{
    public partial class MainPage : PhoneApplicationPage
    {
        // Constructor
        public MainPage()
        {
            InitializeComponent();

            // Set the data context of the listbox control to the sample data
            DataContext = App.ViewModel;
            this.Loaded += new RoutedEventHandler(MainPage_Loaded);
        }

        // Handle selection changed on ListBox
        private void MainListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            // If selected index is -1 (no selection) do nothing
            if (MainListBox.SelectedIndex == -1)
                return;

            // Navigate to the new page
            NavigationService.Navigate(new Uri("/DetailsPage.xaml?selectedItem=" + MainListBox.SelectedIndex, UriKind.Relative));

            // Reset selected index to -1 (no selection)
            MainListBox.SelectedIndex = -1;
        }

        // Load data for the ViewModel Items
        private void MainPage_Loaded(object sender, RoutedEventArgs e)
        {

            string shareDoveRest = @"http://sharedove.cld.sr/sahreconf2012/_vti_bin/ShareDoveRestService/ShareDove.svc/anonymous/GetSessions";
        
            RestClient client = new RestClient { BaseUrl = shareDoveRest };

            var request = new RestRequest { RequestFormat = DataFormat.Xml };

            client.ExecuteAsync<Sessions>(request, response =>
            {
                readSessions(response);
            });



        }

        /// <summary>
        /// Deserialize sessions
        /// </summary>
        /// <param name="response"></param>
        private void readSessions(IRestResponse<Sessions> response)
        {
            try
            {
                if (response.StatusCode == System.Net.HttpStatusCode.OK)
                {
                    System.Runtime.Serialization.Json.DataContractJsonSerializer deserializer = new System.Runtime.Serialization.Json.DataContractJsonSerializer(typeof(Sessions));

                    Sessions sessions = deserializer.ReadObject(new System.IO.MemoryStream(response.RawBytes)) as Sessions;

                    App.ViewModel.RestData = sessions;

                    if (!App.ViewModel.IsDataLoaded)
                    {
                        App.ViewModel.LoadData();
                    }

                }
                else
                {
                    throw new Exception("Could not deserialize sessions");
                }
            }
            catch (Exception ex)
            {
                throw new Exception("Exception in deserializing ShareDove data", ex); 
            }
        }

    

    }
}